<?php

use backend\components\widgets\RegionWidget;
use common\models\c2\entity\Region;
use yii\helpers\Html;
use kartik\widgets\ActiveForm;
use kartik\builder\Form;
use cza\base\widgets\ui\adminlte2\InfoBox;
use cza\base\models\statics\EntityModelStatus;

$regularLangName = \Yii::$app->czaHelper->getRegularLangName();
$messageName = $model->getMessageName();
?>

<?php
$form = ActiveForm::begin([
    'action' => ['edit', 'id' => $model->id],
    'options' => [
        'id' => $model->getBaseFormName(),
        'data-pjax' => true,
    ]]);
?>

    <div class="<?= $model->getPrefixName('form') ?>
">
        <?php if (Yii::$app->session->hasFlash($messageName)): ?>
            <?php if (!$model->hasErrors()) {
                echo InfoBox::widget([
                    'withWrapper' => false,
                    'messages' => Yii::$app->session->getFlash($messageName),
                ]);
            } else {
                echo InfoBox::widget([
                    'defaultMessageType' => InfoBox::TYPE_WARNING,
                    'messages' => Yii::$app->session->getFlash($messageName),
                ]);
            }
            ?>
        <?php endif; ?>

        <div class="well">
            <?php
            // $url = \yii\helpers\Url::toRoute(['get-region']);

            echo Form::widget([
                'model' => $model,
                'form' => $form,
                'columns' => 1,
                'attributes' => [
                    'name' => ['type' => Form::INPUT_TEXT, 'options' => ['placeholder' => $model->getAttributeLabel('name')]],
                    // 'area' => ['type' => Form::INPUT_TEXT, 'options' => ['placeholder' => $model->getAttributeLabel('area')]],
                    // 'area' => [
                    //     'type' => Form::INPUT_WIDGET,
                    //     'widgetClass' => RegionWidget::className(),
                    //     'options' => [
                    //         'url' => $url,
                    //         'province' => [
                    //             'attribute' => 'province',
                    //             'items' => \common\models\c2\entity\Region::getRegion(),
                    //             'options' => ['style' => 'flex:1', 'class' => 'form-control form-control-inline', 'prompt' => '选择省份']
                    //
                    //         ],
                    //         'city' => [
                    //             'attribute' => 'city',
                    //             'items' => \common\models\c2\entity\Region::getRegion($model['province']),
                    //             'options' => ['style' => 'flex:1', 'class' => 'form-control form-control-inline', 'prompt' => '选择城市']
                    //         ],
                    //         'district' => [
                    //             'attribute' => 'district',
                    //             'items' => \common\models\c2\entity\Region::getRegion($model['city']),
                    //             'options' => ['style' => 'flex:1', 'class' => 'form-control form-control-inline', 'prompt' => '选择县/区']
                    //         ],
                    //     ]
                    // ],
                ]
            ]);

            echo $form->field($model, 'area', [
                'addon' => [
                    'append' => ['content' => '<button id="areabtn" type="button" class="btn btn-secondary btn-default">' . Yii::t('app.c2', 'Select') . '</button>', 'asButton' => true]
                ]
            ]);

            echo Form::widget([
                'model' => $model,
                'form' => $form,
                'columns' => 3,
                'attributes' => [
                    'address' => ['type' => Form::INPUT_TEXT, 'options' => ['placeholder' => $model->getAttributeLabel('address')]],
                    'status' => ['type' => Form::INPUT_DROPDOWN_LIST, 'items' => EntityModelStatus::getHashMap('id', 'label')],
                    'cc_room_sum' => ['type' => Form::INPUT_TEXT, 'options' => ['type' => 'number', 'placeholder' => $model->getAttributeLabel('cc_room_sum')]],
                    'unit_sum' => ['type' => Form::INPUT_TEXT, 'options' => ['type' => 'number', 'placeholder' => $model->getAttributeLabel('unit_sum')]],
                    'wall_psum' => ['type' => Form::INPUT_TEXT, 'options' => ['type' => 'number', 'placeholder' => $model->getAttributeLabel('wall_psum')]],
                    'umber_start' => ['type' => Form::INPUT_TEXT, 'options' => ['type' => 'number', 'placeholder' => $model->getAttributeLabel('umber_start')]],
                    'umber_end' => ['type' => Form::INPUT_TEXT, 'options' => ['type' => 'number', 'placeholder' => $model->getAttributeLabel('umber_end')]],
                    'umber_unit' => ['type' => Form::INPUT_TEXT, 'options' => ['placeholder' => $model->getAttributeLabel('umber_unit')]],
                    'units' => ['type' => Form::INPUT_TEXT, 'options' => ['type' => 'number', 'placeholder' => $model->getAttributeLabel('units')]],
                    'total_floor' => ['type' => Form::INPUT_TEXT, 'options' => ['type' => 'number', 'placeholder' => $model->getAttributeLabel('total_floor')]],
                    'th_per_floor' => ['type' => Form::INPUT_TEXT, 'options' => ['type' => 'number', 'placeholder' => $model->getAttributeLabel('th_per_floor')]],
                    'remarks' => ['type' => Form::INPUT_TEXT, 'options' => ['placeholder' => $model->getAttributeLabel('remarks')]],
                    'user_name' => ['type' => Form::INPUT_TEXT, 'options' => ['placeholder' => $model->getAttributeLabel('user_name')]],
                    'password' => ['type' => Form::INPUT_PASSWORD, 'options' => ['placeholder' => $model->getAttributeLabel('password')]],
                    'contact' => ['type' => Form::INPUT_TEXT, 'options' => ['placeholder' => $model->getAttributeLabel('contact')]],
                    'phone' => ['type' => Form::INPUT_TEXT, 'options' => ['placeholder' => $model->getAttributeLabel('phone')]],
                ]
            ]);

            echo Html::beginTag('div', ['class' => 'box-footer']);
            echo Html::submitButton('<i class="fa fa-save"></i> ' . Yii::t('app.c2', 'Save'), ['type' => 'button', 'class' => 'btn btn-primary pull-right']);
            echo Html::a('<i class="fa fa-arrow-left"></i> ' . Yii::t('app.c2', 'Go Back'), ['index'], ['data-pjax' => '0', 'class' => 'btn btn-default pull-right', 'title' => Yii::t('app.c2', 'Go Back'),]);
            echo Html::endTag('div');
            ?>

        </div>
    </div>
<?php ActiveForm::end(); ?>
<?php

$url = \yii\helpers\Url::toRoute(['get-region']);

\yii\bootstrap\Modal::begin([
    'id' => 'content-edit',
    'size' => 'modal-lg',
]);
echo $form->field($model, 'province')->widget(RegionWidget::className(), [
    'model' => $model,
    'url' => $url,
    'province' => [
        'attribute' => 'province',
        'items' => Region::getRegion(),
        'options' => ['class' => 'form-control form-control-inline', 'prompt' => '选择省份']
    ],
    'city' => [
        'attribute' => 'city',
        'items' => Region::getRegion($model['province']),
        'options' => ['class' => 'form-control form-control-inline', 'prompt' => '选择城市']
    ],
    'district' => [
        'attribute' => 'district',
        'items' => Region::getRegion($model['city']),
        'options' => ['class' => 'form-control form-control-inline', 'prompt' => '选择县/区']
    ]
]);

echo Html::beginTag('div', ['class' => 'box-footer']);
echo Html::button('<i class="fa fa-save"></i> ' . Yii::t('app.c2', 'Save'), ['type' => 'button', 'id' => 'selectsave', 'class' => 'btn btn-primary pull-right']);
echo Html::endTag('div');
\yii\bootstrap\Modal::end();

$js = "";

$js .= "jQuery(document).off('click', 'button#areabtn').on('click', 'button#areabtn', function(e) {
            e.preventDefault();
            jQuery('#content-edit').modal('show');
        });";
$js .= "jQuery(document).off('click', 'button#selectsave').on('click', 'button#selectsave', function(e) {
            e.preventDefault();
            let t1 = $('#community-province').find('option:selected').text();
            let t2 = $('#community-city').find('option:selected').text();
            let t3 = $('#community-district').find('option:selected').text();
            $('#community-area').val(t1 + ',' + t2 + ',' + t3);
            jQuery('#content-edit').modal('hide');
        });";

$this->registerJs($js);

?>